layui.use(['form','layer','layedit','laydate','upload'],function(){
    var form = layui.form
    layer = parent.layer === undefined ? layui.layer : top.layer,
        laypage = layui.laypage,
        upload = layui.upload,
        layedit = layui.layedit,
        laydate = layui.laydate;
        $ = layui.jquery;

    //普通图片上传
    var uploadInst = upload.render({
        elem: '#test1'
        ,url: '/admin/Upload/uploadImg'
        ,before: function(obj){
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                $('#demo1').attr('src', result); //图片链接（base64）
            });
        }
        ,done: function(res){
            console.log(res);
            //如果上传失败
            if(res.code > 0){
                return layer.msg(res.msg);
            }
            //上传成功
            $('#imgID').html('<input type="hidden" name ="img_id" value="'+res.img+'">');
        }
        ,error: function(){
            //演示失败状态，并实现重传
            var demoText = $('#demoText');
            demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
            demoText.find('.demo-reload').on('click', function(){
                uploadInst.upload();
            });
        }
    });


    //登录按钮
    form.on("submit(submit)",function(data){
        console.log(data.field);
        $.ajax({
            type: "Post",
            url: "addHouseType",
            dataType: "json",
            data:data.field,
            success: function(msg){
                if(msg.code == '-1'){
                    layer.msg(msg.msg,{time:0.6*1000});
                }
                if(msg.code =='1'){

                    window.parent.location.reload();
                    var index = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index);
                }
            }
        });
        return false;
    })



})